﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class MonHangDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {

            MonHangDTO objectDTO = new MonHangDTO();
            objectDTO.MaMonHang = int.Parse(dt.Rows[i]["MaMonHang "].ToString());
            objectDTO.TenMonHang = dt.Rows[i]["TenMonHang"].ToString();
            objectDTO.MaKhachHangGui = int.Parse(dt.Rows[i]["MaKhachHangGui"].ToString());
            objectDTO.KichThuoc = dt.Rows[i]["KichThuoc"].ToString();
            objectDTO.TrongLuong = float.Parse(dt.Rows[i]["TrongLuong"].ToString());
            objectDTO.DiaChiNhan = dt.Rows[i]["DiaChiNhan"].ToString();
            objectDTO.TenNguoiNhan = dt.Rows[i]["TenNguoiNhan"].ToString();
            objectDTO.CMNDNguoiNhan = dt.Rows[i]["CMNDNguoiNhan"].ToString();
            objectDTO.MaDiaDiem = int.Parse(dt.Rows[i]["MaDiaDiem"].ToString());
            objectDTO.NgayKhachNhanHang = DateTime.Parse(dt.Rows[i]["NgayKhachNhanHang"].ToString());
            objectDTO.Nhan = int.Parse(dt.Rows[i]["Nhan"].ToString());
            return (object)objectDTO;
        }

        public MonHangDTO[] GetAll()
        {
            String strSQL = "Select * from MONHANG,KHACHHANG Where MONHANG.MaKhachHangGui = KHACHHANG.MaKhachHang ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            MonHangDTO[] monhangDTOs = new MonHangDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                monhangDTOs[i] = (MonHangDTO)GetDataFromDataRow(dt, i);

            return monhangDTOs;
        }

        public string insert(MonHangDTO info, KhoDTO info1, DatVanChuyenDTO info2)
        {
            bool flag = false;
            string insertCommand = "INSERT INTO MONHANG(TenMonHang,MaKhachHangGui,KichThuoc,TrongLuong,DiaChiNhan,TenNguoiNhan,CMNDNguoiNhan,MaDiaDiem,Nhan) VALUES('" +
                info.TenMonHang + "', " +
                info.MaKhachHangGui + ", '" +
                info.KichThuoc + "', " +
                info.TrongLuong + ", '" +
                info.DiaChiNhan + "', '" +
                info.TenNguoiNhan + "', '" +
                info.CMNDNguoiNhan + "', " +
                info.MaDiaDiem + "," +
                //info.NgayKhachNhanHang.ToShortDateString() + "', " +
                info.Nhan + ")";

            flag = ExecuteNonQuery(insertCommand);
            int MaMonHang = ExcuteScalar("Select max(MaMonHang) From MONHANG");
            info2.MaMonHang = MaMonHang;
            int MaDatVanChuyen = 0;
            if (flag)
            {

                string insertCommand1 = "INSERT KHO_CT VALUES(" +
                info1.MaKho + ", " +
                MaMonHang + ", " +
                info1.Gui + ")";

                flag = ExecuteNonQuery(insertCommand1);
                // string strSQL = "Insert into DATVANCHUYEN(MaMonHang,NgayLayHang,DiaChiLayHang,MaLoaiVC,MaDiaDiem,)" +
                //     " Values(" + MaMonHang + ",'" + info2.NgayLayHang + "','" + info2.DiaChiLayHang +"'," +
                //    info2.MaLoaiVC + "," + info2.MaDiaDiem + ")";
                flag = new DatVanChuyenDAO().insert(info2);
                MaDatVanChuyen = ExcuteScalar("Select max(MaDatVanChuyen) From DATVANCHUYEN");
                // flag = ExecuteNonQuery(strSQL);
            }
            string strReturn = MaMonHang + "," + MaDatVanChuyen;
            return flag == true ? strReturn : "-1";
        }

        public void update(MonHangDTO info, KhoDTO info1)
        {
            string updateCommand = "UPDATE MONHANG " +
                                    "SET TenMonHang = '" + info.TenMonHang + "', " +
                                    " MaKhachHangGui = " + info.MaKhachHangGui + ", " +
                                    " KichThuoc = '" + info.KichThuoc + "', " +
                                    " TrongLuong = " + info.TrongLuong + ", " +
                                    " DiaChiNhan = '" + info.DiaChiNhan + "', " +
                                    " TenNguoiNhan = '" + info.TenNguoiNhan + "', " +
                                    " CMNDNguoiNhan = '" + info.CMNDNguoiNhan + "', " +
                                    " MaDiaDiem = " + info.MaDiaDiem + ", " +
                                    " NgayKhachNhanHang = '" + info.NgayKhachNhanHang.ToShortDateString() + "', " +
                                    " Nhan = " + info.Nhan +
                                    " WHERE MaMonHang = " + info.MaMonHang;


            ExecuteNonQuery(updateCommand);

            string updateCommand1 = "UPDATE KHO_CT " +
                        "SET MaKho = " + info1.MaKho + ", " +
                        " Gui = '" + info1.Gui + "', " +
                        " WHERE MaDatVanChuyen = " + info1.MaMonHang;


            ExecuteNonQuery(updateCommand1);
        }

        //public void delete(string maDatVanChuyen)
        //{
        //    string deleteCommand = "DELETE FROM DATVANCHUYEN WHERE MaDatVanChuyen = " + maDatVanChuyen;
        //    ExecuteNonQuery(deleteCommand);
        //}
    }
}
